package actionForm;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class CreateContactValidationForm extends ActionForm {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String firstName=null;
	private String lastName=null;
	private String email=null;
	private String street=null;
	private String city=null;
	private String zip=null;
	private String country=null;
	private String numSiret=null;
	private String society=null;
	private String phoneNumber = null;
	private String phoneKind = null;
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getSociety() {
		return society;
	}
	public void setSociety(String society) {
		this.society = society;
	}
	private String[] groupIds = null;
	
	
	public String[] getGroupIds() {
		return groupIds;
	}
	public void setGroupIds(String[] groupIds) {
		this.groupIds = groupIds;
	}
	/**
	 * @return Email
	 */
	public String getEmail() {
		return email;
	}
	/**
	 * @return First Name
	 */
	public String getFirstName() {
		return firstName;
	}
	/**
	 * @return Last name
	 */
	public String getLastName() {
		return lastName;
	}
	/**
	 * @param string Sets the Email
	 */
	public void setEmail(String string) {
		email = string;
	}
	/**
	 * @param string Sets the First Name
	 */
	public void setFirstName(String string) {
		firstName = string;
	}
	/**
	 * @param string sets the Last Name
	 */
	public void setLastName(String string) {
		lastName = string;
	}
	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getNumSiret() {
		return numSiret;
	}

	public void setNumSiret(String numSiret) {
		this.numSiret = numSiret;
	}
	public String getPhoneKind() {
		return phoneKind;
	}
	public void setPhoneKind(String phoneKind) {
		this.phoneKind = phoneKind;
	}
	public void reset(ActionMapping mapping, HttpServletRequest request) {
//		this.firstName=null;
//		this.lastName=null;
//		this.email=null;
		
	}
	public ActionErrors validate(
			ActionMapping mapping, HttpServletRequest request ) {
		ActionErrors errors = new ActionErrors();
		if( getFirstName()== null || getFirstName().length() < 1 ) {
			errors.add("first name",new
					ActionMessage("creation.fn.error.required"));
		}
		if( getLastName()== null || getLastName().length() < 1 ) {
			errors.add("last name",new
					ActionMessage("creation.ln.error.required"));
		}
		if( getEmail() == null || getEmail().length() < 1 ) {
			errors.add("email", new
					ActionMessage("creation.email.error.required"));
		}
		Pattern rfc2822 = Pattern.compile(
		        "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"
		);

		if (!rfc2822.matcher(email).matches()) {
			errors.add("email", new
					ActionMessage("creation.email.error.invalid"));
		}
		if( getStreet() == null || getStreet().length() < 1 ) {
			errors.add("street", new
					ActionMessage("creation.street.error.required"));
		}
		if( getCity()== null || getCity().length() < 1 ) {
			errors.add("city",new
					ActionMessage("creation.city.error.required"));
		}
		if( getZip()== null || getZip().length() < 1 ) {
			errors.add("zip",new
					ActionMessage("creation.zip.error.required"));
		}
		try {
				Integer.parseInt(getZip());
		} catch (NumberFormatException e) {
			errors.add("zip", new
					ActionMessage("creation.zip.error.format"));
		}
		if( getCountry() == null || getCountry().length() < 1 ) {
			errors.add("country", new
					ActionMessage("creation.country.error.required"));
		}
		try {
			if(getNumSiret() != null && getSociety() != null){
				Integer.parseInt(getNumSiret());
			}
		} catch (NumberFormatException e) {
			errors.add("num siret", new
					ActionMessage("creation.numsiret.error.format"));
		}
		return errors;
	}
}